package com.atguigu.lease.web.admin.controller.apartment;


import com.atguigu.lease.common.result.Result;
import com.atguigu.lease.model.entity.AttrKey;
import com.atguigu.lease.model.entity.AttrValue;
import com.atguigu.lease.web.admin.service.AttrKeyService;
import com.atguigu.lease.web.admin.service.AttrValueService;
import com.atguigu.lease.web.admin.vo.attr.AttrKeyVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


@Tag(name = "房间属性管理")
@RestController
@RequestMapping("/admin/attr")
public class AttrController {

    @Autowired
    AttrKeyService attrKeyService;

    @Autowired
    AttrValueService attrValueService;

    @Operation(summary = "新增或更新属性名称")
    @PostMapping("key/saveOrUpdate")
    public Result saveOrUpdateAttrKey(@RequestBody AttrKey attrKey) {

        Long id = attrKey.getId();

        if(id!=null){
            attrKey.setUpdateTime(null);
        }
        attrKeyService.saveOrUpdate(attrKey);

        return Result.ok();
    }

    @Operation(summary = "新增或更新属性值")
    @PostMapping("value/saveOrUpdate")
    public Result saveOrUpdateAttrValue(@RequestBody AttrValue attrValue) {
        Long id = attrValue.getId();
        if(id!=null){
            attrValue.setUpdateTime(null);
        }
        attrValueService.saveOrUpdate(attrValue);
        return Result.ok();
    }


    @Operation(summary = "查询全部属性名称和属性值列表")
    @GetMapping("list")
    public Result<List<AttrKeyVo>> listAttrInfo() {
//        List<AttrKey> attrList = attrKeyService.list();
//        List<AttrKeyVo> attrKeyVos = new ArrayList<>();
//        for (AttrKey attrKey : attrList) {
//            AttrKeyVo attrKeyVo = new AttrKeyVo();
//            BeanUtils.copyProperties(attrKey,attrKeyVo);
//            Long attrId = attrKey.getId();
//            QueryWrapper<AttrValue> attrValueQueryWrapper = new QueryWrapper<>();
//            attrValueQueryWrapper.eq("attr_key_id",attrId);
//            List<AttrValue> valueList = attrValueService.list(attrValueQueryWrapper);
//            attrKeyVo.setAttrValueList(valueList);
//            attrKeyVos.add(attrKeyVo);
//        }
        List<AttrKeyVo> attrKeyVos = attrKeyService.listAttrInfo();
        return Result.ok(attrKeyVos);
    }

    @Operation(summary = "根据id删除属性名称")
    @DeleteMapping("key/deleteById")
    public Result removeAttrKeyById(@RequestParam Long attrKeyId) {

        // 先删除该属性的值
        QueryWrapper<AttrValue> attrValueQueryWrapper = new QueryWrapper<>();
        attrValueQueryWrapper.eq("attr_key_id",attrKeyId);
        List<AttrValue> valueList = attrValueService.list(attrValueQueryWrapper);
        if(valueList!= null&&valueList.size()>0){
            attrValueQueryWrapper.clear();// 一个条件构造器第二次使用前要清理前一次的条件
            List<Long> ids = valueList.stream().map(value -> value.getId()).collect(Collectors.toList());
            attrValueQueryWrapper.in("id",ids);// 删除所有对应的id的数据
            attrValueService.remove(attrValueQueryWrapper);
        }

        // 再删除该属性
        attrKeyService.removeById(attrKeyId);

        return Result.ok();
    }

    @Operation(summary = "根据id删除属性值")
    @DeleteMapping("value/deleteById")
    public Result removeAttrValueById(@RequestParam Long id) {
        attrValueService.removeById(id);
        return Result.ok();
    }

}
